import { defineStore } from 'pinia'
import { store } from '../index'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import { ElMessage, ElMessageBox } from 'element-plus'
import * as AgentApi from '@/api/call/agent'
const { wsCache } = useCache()
import WebPhone from '@/hooks/web/useCall'
import { emitter } from '@/utils/eventBus.ts'

export const usePhoneStore = defineStore('phone', {
  state: () => {
    return {
      channelId: null, // 外呼渠道id，从登录用户中获取
      phoneModeList: [
        {
          name: 'SIP模式',
          value: 'sip'
        },
        {
          name: '双向回拨模式',
          value: 'recall'
        },
        {
          name: '无线话机模式',
          value: 'box'
        },
        {
          name: '手机SIM卡',
          value: 'sim'
        }
      ],
      phoneMode: null, //
      session: null,
      callBoardVisible: false,
      // 外呼任务窗口状态
      callTaskVisible: false,
      taskInfo: null,
      agentStatus: null,
      agentNumber: null,
      autoAnswer: false,
      isDialog: false,
      automatic: false,
      needCall: false,
      notificationNum: 0,
      sipRegSuccess: false,
      incomVolume: 100,
      callWorkSize: 'none' //工作台窗口:  none:隐藏, mini:最小化, primary:正常窗口, large:最大化
    }
  },
  getters: {
    getIncomVolume() {
      return this.incomVolume
    },
    getCallWorkSize() {
      return this.callWorkSize
    },
    getSipRegSuccess() {
      return this.sipRegSuccess
    },
    getNotificationNum() {
      return this.notificationNum
    },
    getNeedCall() {
      return this.needCall
    },
    getAutomatic() {
      return this.automatic
    },
    getIsDialog() {
      return this.isDialog
    },
    getAutoAnswer() {
      return this.autoAnswer
    },
    getAgentNumber() {
      return this.agentNumber
    },
    getAgentStatus() {
      return this.agentStatus
    },
    getCallTaskVisible() {
      return this.callTaskVisible
    },
    getTaskInfo() {
      return this.taskInfo
    },
    getPhoneModeList() {
      return this.phoneModeList
    },
    getPhoneMode() {
      return this.phoneMode
    },
    getChannelId() {
      return this.channelId
    },
    getSession() {
      return this.session
    },
    getCallBoardVisible() {
      return this.callBoardVisible
    }
  },
  actions: {
    async setIncomVolume(incomVolume) {
      this.incomVolume = incomVolume
    },
    async setCallWorkSize(callWorkSize) {
      this.callWorkSize = callWorkSize
    },
    async setSipRegSuccess(sipRegSuccess) {
      this.sipRegSuccess = sipRegSuccess
    },
    async setNotificationNum(notificationNum) {
      this.notificationNum++
    },
    async setNeedCall(needCall) {
      this.needCall = needCall
    },
    async setAutomatic(automatic) {
      this.automatic = automatic
    },
    async setIsDialog(isDialog) {
      this.isDialog = isDialog
    },
    async setAutoAnswer(autoAnswer) {
      this.autoAnswer = autoAnswer
    },
    async setAgentNumber(agentNumber) {
      this.agentNumber = agentNumber
    },
    async setTaskInfo(taskInfo) {
      this.taskInfo = taskInfo
    },
    async setCallTaskVisible(visible) {
      this.callTaskVisible = visible
    },
    async setVisible(payload) {
      this.callBoardVisible = payload
    },
    async clearAll() {
      this.phoneMode = null
      this.channelId = null
    },
    async setAgentStatus(status) {
      this.agentStatus = status
    },
    async setPhoneMode(phoneMode) {
      const data = await AgentApi.getChannelId({ phoneMode })
      if (phoneMode != 'sip') {
        // 注册成功了切换别的模式需要清空
        if (this.sipRegSuccess) {
          WebPhone.getInstance().setLogout()
          this.sipRegSuccess = false
        }
      }
      // 修改： 根据phoneMode 去查找channelId -> 然后把channelId 返回到本地更新
      if (data) {
        this.phoneMode = phoneMode
        this.channelId = data
      } else {
        ElMessage.error('该企业未激活此外呼模式，请联系管理员开通')
      }
    },
    async setChannelId(channelId) {
      this.channelId = channelId
      // wsCache.set(CACHE_KEY.ENT_TAG, data)
    },
    async setSession(data) {
      this.session = data
    }
  }
})

export const usePhoneStoreWithOut = () => {
  return usePhoneStore(store)
}
